package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.RefTable;
import com.zretc.emptraining.service.RefTableService;
import com.zretc.emptraining.serviceimpl.PerCourseServiceImp;
import com.zretc.emptraining.serviceimpl.RefTableServiceImp;

/**
 * Servlet implementation class RefTableServlet
 */
@WebServlet("/RefTableServlet")
public class RefTableServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	RefTableService rtsi = new RefTableServiceImp();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RefTableServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset = utf-8");
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);
		if (op.equals("showRefTableByPage")) {
			// 查询参培所有信息   -- 包含根据不同条件的查询
			showRefTableByPage(request, response);
			// 删除参培信息
		} else if (op.equals("delRefTable")) {
			delRef(request, response);
			// 添加参培信息
		} else if (op.equals("addRefTable")) {
			addRef(request, response);
			// 修改参培信息
		} else if (op.equals("updateRefTable")) {
			updRef(request, response);
			// 批量删除
		} else if (op.equals("delAll")) {
			delAll(request, response);
			// 主管前台页面上的参培表
		} else if (op.equals("showRefTableByTeach")) {
			showRefByTeach(request, response);
			//前台页面根据课程申请编号来查询的
		} else if (op.equals("showRefTableByCpNo")) {
			showRefByCpNo(request, response);
			// 获取一门课的参加员工数
		} else if (op.equals("getCountByCpNo")) {
			getCount(request, response);
			//参培数据导出到Excel的ajax请求
		} else if (op.equals("showRefTableByExcel")) {
			showRefTableByExcel(request, response);
			//前台员工详情页面的人数
		} else if (op.equals("ajaxCountReq")) {
			ajaxCountReq(request, response);
			
		}
	}

	private void ajaxCountReq(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		System.out.println("cc"+cpNo);
		int count = rtsi.getCountByCPNo(cpNo);
		System.out.println("人数"+count);
		Gson gson = new Gson();
		String jsonString = gson.toJson(count);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	private void showRefTableByExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PageData<RefTable> pd = new PageData<RefTable>();
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		int page = 1;
		int pageSize = 10000;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		pd = rtsi.getRefTabelBycpNo(page, pageSize, cpNo);
		Gson gson = new Gson();
		String gsonString = gson.toJson(pd);
		PrintWriter printWriter =response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	private void getCount(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int count = rtsi.getCountByCPNo(1);
		request.setAttribute("count", count);
		request.getRequestDispatcher("my-score.jsp").forward(request, response);
	}

	private void showRefByCpNo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String courseName = request.getParameter("courseName");
		System.out.println("课程名"+courseName);
		String empno = "";
		if (request.getParameter("empNo")!=null) {
			empno = request.getParameter("empNo");
		}
		System.out.println("员工号"+empno);		;
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		System.out.println("cpno"+cpNo);
		if (empno == null || empno.equals("")) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelBycpNo(page, pageSize, cpNo);
			request.setAttribute("pd", pd);
			request.setAttribute("courseName", courseName);
			request.setAttribute("cpNo", cpNo);
			request.getRequestDispatcher("emp-par.jsp").forward(request, response);
		} else if (empno != null) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			System.out.println("test");
			System.out.println("empNo:" + empno);
			int empNo = Integer.parseInt(empno);
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelBycpNoWithEmpNo(page, pageSize, cpNo, empNo);
			request.setAttribute("pd", pd);
			request.setAttribute("empNo", empNo);
			request.setAttribute("cpNo", cpNo);
			request.setAttribute("courseName", courseName);
			request.getRequestDispatcher("emp-par.jsp").forward(request, response);
		}
	}

	private void showRefByTeach(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String empno = request.getParameter("empNo");
	
		int deptNo = Integer.parseInt(request.getParameter("deptNo"));
		
			
		
		System.out.println(deptNo);
		if (empno == null || empno.equals("")) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelBydeptNo(page, pageSize, deptNo);
			request.setAttribute("pd", pd);
			request.getRequestDispatcher("reftable.jsp").forward(request, response);
		} else if (empno != null) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
		
			int empNo = Integer.parseInt(empno);
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelBydeptNoWithEmpNo(page, pageSize, deptNo, empNo);
			request.setAttribute("pd", pd);
			request.setAttribute("empNo", empNo);
			request.getRequestDispatcher("reftable.jsp").forward(request, response);
		}
	}

	private void delAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String deleteAll = request.getParameter("deleteAll");
		System.out.println("deleteall:" + deleteAll);
		deleteAll = deleteAll.substring(0, deleteAll.length() - 1);
		String[] strs = deleteAll.split(",");
		boolean flag = false;
		for (int i = 0; i < strs.length; i++) {
			flag = rtsi.deleteRefTable((Integer.parseInt(strs[i])));
		}
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	private void updRef(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int course = Integer.parseInt(request.getParameter("cpNo"));
		int refTableNo = Integer.parseInt(request.getParameter("refTableNo"));
//			String courseTime = request.getParameter("courseTime");
		RefTable refTable = new RefTable(refTableNo, empNo, course);
		boolean flag = rtsi.updateRefTable(refTable);
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	private void addRef(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		// diae是判断同一节课是不是有两个相同的empNo在表中
		boolean flag1 = rtsi.diae(cpNo, empNo);
		PerCourseServiceImp pcsi = new PerCourseServiceImp();
		boolean flag2 = pcsi.wExists(empNo, cpNo);
		// 两个表中都没有员工的信息才能进行添加，其中一个表中有则不能进行添加
		if (flag1 == true && flag2 == true) {
			RefTable refTable = new RefTable(empNo, cpNo);
			boolean flag = rtsi.addRefTable(refTable);
			System.out.println("flag:" + flag);
			Gson gson = new Gson();
			String gsonString = gson.toJson(flag);
			PrintWriter printWriter = response.getWriter();
			printWriter.print(gsonString);
			printWriter.close();
		} else {
			boolean flag3 = flag1 == true && flag2 == true;
			Gson gson = new Gson();
			String gsonString = gson.toJson(flag3);
			PrintWriter printWriter = response.getWriter();
			printWriter.print(gsonString);
			printWriter.close();
		}
	}

	private void delRef(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int refTableNo = Integer.parseInt(request.getParameter("refTableNo"));
		boolean flag = rtsi.deleteRefTable(refTableNo);
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	private void showRefTableByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String select = "";
		if (request.getParameter("select") != null) {
			select = request.getParameter("select");
		}
		String keyword = "";
		if(request.getParameter("key")!=null) {
			keyword = request.getParameter("key");
		}
		
		// 查询参培表的所有信息
		if (select.equals("") || keyword.equals("")) {
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelByPage(page, pageSize);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("RefTableList.jsp").forward(request, response);
			// 根据员工编号查询参培信息
		} else if (select.equals("员工号")) {
			int key = Integer.parseInt(keyword);
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelByEmpNo(page, pageSize, key);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.setAttribute("key", key);
			request.getRequestDispatcher("RefTableList.jsp").forward(request, response);
			// 根据课程计划号查询参培信息
		} else if (select.equals("课程计划号")) {
			int key = Integer.parseInt(keyword);
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelBycpNo(page, pageSize, key);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("RefTableList.jsp").forward(request, response);
		} else if (select.contentEquals("deptNo")) {
			int key = Integer.parseInt(request.getParameter("key"));
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<RefTable> pd = new PageData<RefTable>();
			pd = rtsi.getRefTabelBydeptNo(page, pageSize, key);
			request.setAttribute("pd", pd);
			request.setAttribute("select", select);
			request.getRequestDispatcher("RefTableList.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
